﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using System.Collections;

namespace Prototype1.__AdminCompany
{
    public partial class WF_AddUsersToDepartment : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
            }
            else
            {
                //init all drop boxes 
                LoadAllDepartments();
                ReloadDepLevel(null,null);            
                                
                                
            }

        }

        private void LoadAllUsersFromDepartmentLevel(DepartmentLevel dl)
        {
            this.ListBox_Users.Items.Clear();
            this.ListBox_UsersOnDepartmentLevel.Items.Clear();

            using (DataAccessDataContext dc = new DataAccessDataContext())
            {
                var query = from deplevel in dc.UserDepartments
                            join user in dc.Users on deplevel.iduser equals user.email
                            where deplevel.iddeplevel == dl.idlevel
                            orderby user.Shortname
                            select new { 
                                Email = user.email,
                                ShortName = user.Shortname,
                            };


                var q = from u in dc.Users
                        where !(from ud in dc.UserDepartments
                                where ud.iddeplevel == dl.idlevel
                                select ud.iduser).Contains(u.email)
                        select u;               
            
              

                foreach (var e in query)
                    this.ListBox_UsersOnDepartmentLevel.Items.Add(new ListItem(e.ShortName, e.Email));

                foreach (User x in q)
                    this.ListBox_Users.Items.Add(new ListItem(x.Shortname, x.email));                
            }
        }

      

        private void LoadAllDepartmentLevelsFromDepartment(Department d)
        {
            this.DropDown_DepartmentLevel.Items.Clear();

            using (DataAccessDataContext dc = new DataAccessDataContext())
            {

                var query = from dep in dc.DepartmentLevels
                            where dep.iddep == d.id
                            orderby dep.nivel
                            select dep;

                UserDepartment ud = new UserDepartment();

                foreach (DepartmentLevel dl in query)
                    this.DropDown_DepartmentLevel.Items.Add(new ListItem(dl.nome, dl.idlevel.ToString()));

            }
        }

        private void LoadAllDepartments()
        {
            using (DataAccessDataContext dc = new DataAccessDataContext())
            {

                var query = from dep in dc.Departments
                            //where dep.iddep == id
                            orderby dep.nome
                            select dep;
                
                UserDepartment ud = new UserDepartment();

                foreach (Department d in query)
                    this.DropDown_Departments.Items.Add(new ListItem(d.nome, d.id.ToString()));
                
            }
        }

        protected void AddUserToDepartmentLevel(object sender, EventArgs e)
        {
            if (this.ListBox_Users.Items.Count != 0 && this.ListBox_Users.SelectedIndex > -1)
            {
                UserDepartment u = new UserDepartment();
                u.iduser = this.ListBox_Users.SelectedValue;
                u.iddeplevel = new Guid(this.DropDown_DepartmentLevel.SelectedValue);

                using (DataAccessDataContext dc = new DataAccessDataContext())
                {
                    dc.UserDepartments.InsertOnSubmit(u);
                    dc.SubmitChanges();
                }
                
                ListItem x = this.ListBox_Users.SelectedItem;
                this.ListBox_Users.Items.RemoveAt(this.ListBox_Users.SelectedIndex);
                this.ListBox_UsersOnDepartmentLevel.Items.Add(x);

                this.ListBox_UsersOnDepartmentLevel.SelectedIndex = -1;
                this.ListBox_Users.SelectedIndex = -1;
                
            }

        }

        protected void RemoveUserFromDepartmentLevel(object sender, EventArgs e)
        {
            if (this.ListBox_UsersOnDepartmentLevel.Items.Count != 0 && this.ListBox_UsersOnDepartmentLevel.SelectedIndex > -1)
            {
                UserDepartment u = new UserDepartment();
                u.iduser = this.ListBox_UsersOnDepartmentLevel.SelectedValue;
                u.iddeplevel = new Guid(this.DropDown_DepartmentLevel.SelectedValue);

                using (DataAccessDataContext dc = new DataAccessDataContext())
                {
                    UserDepartment ud = dc.UserDepartments.Single(pc =>  pc.iduser == u.iduser && pc.iddeplevel == u.iddeplevel);                                     
                    dc.UserDepartments.DeleteOnSubmit(ud);
                    dc.SubmitChanges();
                }

                ListItem x = this.ListBox_UsersOnDepartmentLevel.SelectedItem;
                this.ListBox_UsersOnDepartmentLevel.Items.RemoveAt(this.ListBox_UsersOnDepartmentLevel.SelectedIndex);
                this.ListBox_Users.Items.Add(x);

                this.ListBox_UsersOnDepartmentLevel.SelectedIndex = -1;
                this.ListBox_Users.SelectedIndex = -1;
            }

        }

        protected void ReloadDepLevel(object sender, EventArgs e)
        {
            if (this.DropDown_Departments.Items.Count != 0)
            {
                Department d = new Department();
                d.id = new Guid(this.DropDown_Departments.SelectedValue);
                LoadAllDepartmentLevelsFromDepartment(d);

                if (this.DropDown_DepartmentLevel.Items.Count != 0)
                {
                    this.DropDown_DepartmentLevel.SelectedIndex = 0;

                    ReloadUsers(null, null);

                }
            }

        }

        protected void ReloadUsers(object sender, EventArgs e)
        {
            DepartmentLevel dl = new DepartmentLevel();
            dl.idlevel = new Guid(this.DropDown_DepartmentLevel.SelectedValue);
            LoadAllUsersFromDepartmentLevel(dl);
        }
    }
}
